{
 "cells": [
  {
   "cell_type": "markdown",
   "id": "f9e8afb1",
   "metadata": {},
   "source": [
    "### 1. 捕获错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "a197627e",
   "metadata": {},
   "outputs": [],
   "source": [
    "def except_function():\n",
    "    try:\n",
    "        10 / 0\n",
    "    except ZeroDivisionError:\n",
    "        print (\"发生除零异常啦.\")\n",
    "    else:\n",
    "        print (\"一切正常啦.\")\n",
    "    finally:\n",
    "        # 无论是否发生异常都将执行最后的代码\n",
    "        print (\"finally必须被执行，不管有没有发生异常.\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "3347b3fd",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "发生除零异常啦.\n",
      "finally必须被执行，不管有没有发生异常.\n"
     ]
    }
   ],
   "source": [
    "except_function()"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "13c3c1f0",
   "metadata": {},
   "source": [
    "### 2. 抛出错误"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "2ef3efaa",
   "metadata": {},
   "outputs": [
    {
     "ename": "Exception",
     "evalue": "错误",
     "output_type": "error",
     "traceback": [
      "\u001b[31m---------------------------------------------------------------------------\u001b[39m",
      "\u001b[31mException\u001b[39m                                 Traceback (most recent call last)",
      "\u001b[36mCell\u001b[39m\u001b[36m \u001b[39m\u001b[32mIn[3]\u001b[39m\u001b[32m, line 2\u001b[39m\n\u001b[32m      1\u001b[39m \u001b[38;5;28;01mif\u001b[39;00m(\u001b[32m1\u001b[39m):\n\u001b[32m----> \u001b[39m\u001b[32m2\u001b[39m     \u001b[38;5;28;01mraise\u001b[39;00m \u001b[38;5;167;01mException\u001b[39;00m(\u001b[33m\"\u001b[39m\u001b[33m错误\u001b[39m\u001b[33m\"\u001b[39m)\n",
      "\u001b[31mException\u001b[39m: 错误"
     ]
    }
   ],
   "source": [
    "if(1):\n",
    "    raise Exception(\"错误\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "28fe5f1d",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "mining",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.12.10"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
